//
//  NativeExpressAdM.m
//  TestDemo
//
//  Created by a on 2023/2/16.
//

#import "NativeExpressAdM.h"
#import <LDMobileAdSDK/LDMobileAdSDK.h>

@interface NativeExpressAdM(UITableView)<UITableViewDelegate,UITableViewDataSource>
- (void)setupTableView:(UITableView *)tableView;
@end

@interface NativeExpressAdM ()<LDNativeExpressAdDelegate>

@property (nonatomic,weak)UITableView * tableView;
@property (nonatomic,strong)NSMutableArray<LDNativeExpressAd *> * ads;
@property (nonatomic,strong)NSMutableArray * expressAdViews;

@end

@implementation NativeExpressAdM

- (void)setupAdInView:(UITableView *)tableView{
    self.tableView = tableView;
    [self setupTableView:tableView];
    [LDSDKConfig setAppKey:@"f7d19924efe93778" token:@"cvvcqdnyrk"];
}

- (void)loadAd{
    if(self.expressAdViews.count > 0) return;
    NSInteger count = 5;
    for (int i = 0 ; i < count; i ++) {
        CGSize adSize = CGSizeMake(self.tableView.bounds.size.width, 0);
        NSLog(@"adSize = %@",NSStringFromCGSize(adSize));
        LDNativeExpressAd * ad = [[LDNativeExpressAd alloc]initWithSlotID:@"0861588" adSize:adSize viewController:self.viewController];
        ad.delegate = self;
        [ad loadAd];
        [self.ads addObject:ad];
    }
}

- (void)cleanAd{
    for (UIView * v in self.expressAdViews) {
        [v removeFromSuperview];
    }
    self.expressAdViews = nil;
    self.ads = nil;
    [self.tableView reloadData];
}

- (NSMutableArray *)expressAdViews{
    if (_expressAdViews == nil) {
        _expressAdViews = @[].mutableCopy;
    }
    return _expressAdViews;
}

- (NSMutableArray *)ads{
    if (_ads == nil) {
        _ads = @[].mutableCopy;
    }
    return _ads;
}

- (void)dealloc{
    NSLog(@"NativeExpressAdM dealloc");
}

#pragma mark -LDNativeExpressAdDelegate
///加载成功
- (void)nativeExpressAdDidLoad:(LDNativeExpressAd*)ad view:(LDNativeExpressAdView *)adView{
    NSLog(@"---------%@",NSStringFromSelector(_cmd));
    [self.expressAdViews addObject:adView];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        for (LDNativeExpressAdView * adView in self.expressAdViews) {
            if ([adView isValid]) {
                [adView render];
            }
        }
    });
}

///广告失败
- (void)nativeExpressAdFail:(LDNativeExpressAd *)ad view:(LDNativeExpressAdView *_Nullable)adView error:(NSError *)error{
    NSLog(@"---------%@ %@",NSStringFromSelector(_cmd),error);
    if(adView){
        [adView removeFromSuperview];
        [self.expressAdViews removeObject:adView];
    }
    [self.ads removeObject:ad];
}

///渲染成功
- (void)nativeExpressAdRenderSucc:(LDNativeExpressAd *)ad view:(LDNativeExpressAdView *)adView{
    NSLog(@"---------%@",NSStringFromSelector(_cmd));
    [self.tableView reloadData];
}
///渲染失败
- (void)nativeExpressAdRenderFail:(LDNativeExpressAd *)ad view:(LDNativeExpressAdView *)adView error:(NSError *)error{
    NSLog(@"---------%@",NSStringFromSelector(_cmd));
}
///曝光
- (void)nativeExpressAdDidExposure:(LDNativeExpressAd *)ad view:(LDNativeExpressAdView *)adView{
    NSLog(@"---------%@",NSStringFromSelector(_cmd));
}
///用户删除广告
- (void)nativeExpressAdDidRemove:(LDNativeExpressAd *)ad view:(LDNativeExpressAdView *)adView{
    NSLog(@"---------%@",NSStringFromSelector(_cmd));
}
///点击广告
- (void)nativeExpressAdClick:(LDNativeExpressAd *)ad view:(LDNativeExpressAdView *)adView{
    NSLog(@"---------%@",NSStringFromSelector(_cmd));
}
///关闭
- (void)nativeExpressAdClose:(LDNativeExpressAd *)ad view:(LDNativeExpressAdView *)adView{
    NSLog(@"---------%@",NSStringFromSelector(_cmd));
    [adView removeFromSuperview];
    [self.expressAdViews removeObject:adView];
    [self.ads removeObject:ad];
    [self.tableView reloadData];
}

@end


@implementation NativeExpressAdM(UITableView)

- (void)setupTableView:(UITableView *)tableView{
    tableView.delegate = self;
    tableView.dataSource = self;
    if (@available(iOS 15.0, *)) {
        tableView.sectionHeaderTopPadding = 0.0;
    } else {
        // Fallback on earlier versions
    }
    tableView.backgroundColor = UIColor.clearColor;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"nativeExpressCell"];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"blankCell"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.expressAdViews.count * 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row % 2 == 0) {
        LDNativeExpressAdView *view = [self.expressAdViews objectAtIndex:indexPath.row / 2];
//        NSLog(@"------------width:%f height: %f",view.bounds.size.width,view.bounds.size.height);
        NSLog(@">>>>>>>view.adSize.height:");
        return view.bounds.size.height;
    }
    else {
        return 50;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    if (indexPath.row % 2 == 0) {
        cell = [self.tableView dequeueReusableCellWithIdentifier:@"nativeExpressCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIView *subView = (UIView *)[cell.contentView viewWithTag:1000];
        if ([subView superview]) {
            [subView removeFromSuperview];
        }
        LDNativeExpressAdView *view = [self.expressAdViews objectAtIndex:indexPath.row / 2];
        view.tag = 1000;
//        NSLog(@"--------cellForRowAtIndexPath frame:%@",NSStringFromCGRect(view.frame));
//        NSLog(@"--------cellForRowAtIndexPath bounds:%@",NSStringFromCGRect(view.bounds));
        [cell.contentView addSubview:view];
//        view.backgroundColor = UIColor.greenColor;
//        view.frame = CGRectMake(-50, 0, view.frame.size.width, view.frame.size.height);
    } else {
        cell = [self.tableView dequeueReusableCellWithIdentifier:@"blankCell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor grayColor];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

@end



